package com.app.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.app.common.Result;
import com.app.model.UserBusinessModel;
import com.app.model.UserModel;
import com.app.service.UserBusinessService;
import com.app.service.UserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.BusinessVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户商家关联(收藏)
 * @author 16693103420
 * @since 2024-01-23
 */
@RestController
@RequestMapping("/userBusiness")
public class UserBusinessController {

    @Autowired
    private UserBusinessService userBusinessService;
    @Autowired
    private UserService userService;

    //用户收藏商家
    @PostMapping("/addUserBusiness")
    public Result addUserBusiness(@RequestBody UserBusinessModel userBusinessModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();

        Integer businessId = userBusinessModel. getBusinessId();
        QueryWrapper<UserBusinessModel> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("business_id", businessId);
        List<UserBusinessModel> list = userBusinessService.list(qw);
        if (CollectionUtil.isNotEmpty(list)) {
            return Result.error(201, "已收藏");
        }
        userBusinessModel.setCreateTime(CommonHelp.getNowTimestamp());
        userBusinessModel.setUserId(userId);
        userBusinessService.save(userBusinessModel);
        return Result.success("收藏成功");
    }

    //查询此用户收藏的商家
    @GetMapping("/getUserBusiness")
    public Result getUserBusiness(Integer page,Integer limit,Double longitude, Double latitude,HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        BusinessVo list = userBusinessService.getUserBusiness(page,limit,userId,longitude,latitude);
        return Result.success(list);
    }

    //用户取消此商家收藏
    @PostMapping("/userStopBusiness")
    public Result userStopBusiness(@RequestBody UserBusinessModel userBusinessModel,HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        Integer businessId = userBusinessModel.getBusinessId();
        QueryWrapper<UserBusinessModel> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("business_id", businessId);
        userBusinessService.remove(qw);
        return Result.success("取消收藏成功");
    }

}

